package com.wind.taobao.api.huabao.convert
{
	import com.wind.taobao.api.huabao.model.PosterPicture;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * PosterPicture 转换器
	 * @author fxy
	 *
	 */
	public class PosterPictureConvert
	{
		/**
		 * 转换PosterPicture集合
		 */
		public static function convertPosterPictures(value:Object, type:String="json"):ArrayCollection
		{
			var posterPictures:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							posterPictures.addItem(convertPosterPicture((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return posterPictures;
		}

		/**
		 * 转换PosterPicture
		 */
		public static function convertPosterPicture(value:Object, type:String="json"):PosterPicture
		{
			var posterPicture:PosterPicture=new PosterPicture();
			if (value == null)
				return posterPicture;
			if (type == TaobaoConstants.XML)
			{
				posterPicture=convertXMLToPosterPicture(XML(value), posterPicture);
			}
			else
			{
				posterPicture=convertJSONToPosterPicture(value, posterPicture);
			}
			return posterPicture;
		}

		/**
		 * JSON转换为PosterPicture
		 */
		private static function convertJSONToPosterPicture(json:Object, posterPicture:PosterPicture):PosterPicture
		{
			if (json.hasOwnProperty(TaobaoConstants.POSTER_ID))
			{
				posterPicture.poster_id=json[TaobaoConstants.POSTER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.ID))
			{
				posterPicture.id=json[TaobaoConstants.ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				posterPicture.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.MODIFIED))
			{
				posterPicture.modified=json[TaobaoConstants.MODIFIED];
			}

			if (json.hasOwnProperty(TaobaoConstants.URL))
			{
				posterPicture.url=json[TaobaoConstants.URL];
			}

			if (json.hasOwnProperty(TaobaoConstants.DESC))
			{
				posterPicture.desc=json[TaobaoConstants.DESC];
			}
			return posterPicture;
		}

		/**
		 * XML转换为PosterPicture
		 */
		private static function convertXMLToPosterPicture(value:XML, posterPicture:PosterPicture):PosterPicture
		{
			return posterPicture;
		}

	}
}